<template>
  <el-dialog
    v-model="visible"
    :title="!dataForm.id ? $t('crud.addTitle') : $t('temp.modify')"
    :close-on-click-modal="false"
    class="dialogWidth"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @submit.prevent
      @keyup.enter="onSubmit()"
    >
      <el-form-item
        :label="$t('shop.announcementTitle')"
        prop="title"
      >
        <el-input
          v-model="dataForm.title"
          maxlength="36"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        :label="$t('product.status')"
        prop="status"
      >
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">
            {{ $t('publics.publicar') }}
          </el-radio>
          <el-radio :label="0">
            {{ $t('publics.cancel') }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('publics.isTop')"
        prop="isTop"
      >
        <el-radio-group v-model="dataForm.isTop">
          <el-radio :label="1">
            {{ $t('publics.yes') }}
          </el-radio>
          <el-radio :label="0">
            {{ $t('publics.no') }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="$t('shop.noticeContent')"
        prop="content"
      >
        <tiny-mce
          v-if="visible"
          ref="contentRef"
          v-model="dataForm.content"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div
          class="default-btn"
          @click="visible = false"
        >
          {{ $t('crud.filter.cancelBtn') }}
        </div>
        <div
          class="primary-btn default-btn"
          @click="onSubmit()"
        >
          {{ $t('crud.filter.submitBtn') }}
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { validHtmlLength, isHtmlNull } from '@/utils/index.js'
import { ElMessage } from 'element-plus'
// eslint-disable-next-line no-unused-vars
const validateTitle = (rule, value, callback) => {
  if (!value.trim()) {
    callback(new Error($t('shop.titCanNoBlank')))
  } else {
    callback()
  }
}
// eslint-disable-next-line no-unused-vars
const validateContent = (rule, value, callback) => {
  if (isHtmlNull(value)) {
    callback(new Error($t('product.content')))
  } else {
    callback()
  }
}
const dataRule = {
  title: [
    { required: true, message: $t('shop.titCanNoBlank'), trigger: 'blur' },
    { validator: validateTitle, trigger: 'blur' }
  ],
  content: [
    { required: true, message: $t('product.content'), trigger: 'blur' },
    { validator: validateContent, trigger: 'blur' }
  ]
}
const visible = ref(false)
const dataForm = ref({
  title: null,
  content: null,
  url: null,
  status: 1,
  isTop: 0
})
const isSubmit = ref(false)
const dataFormRef = ref(null)
const contentRef = ref(null)
const init = id => {
  dataForm.value.id = id || 0
  visible.value = true
  isSubmit.value = false
  nextTick(() => {
    dataFormRef.value?.resetFields()
    if (dataForm.value.id) {
      http({
        url: http.adornUrl('/shop/notice/info/' + dataForm.value.id),
        method: 'get',
        params: http.adornParams()
      }).then(({ data }) => {
        dataForm.value = data
      })
    }
  })
}
const emit = defineEmits(['refreshDataList'])
// 表单提交
const onSubmit = () => {
  dataFormRef.value.validate(valid => {
    if (valid) {
      if (isSubmit.value) {
        return false
      }
      if (validHtmlLength(dataForm.value.content)) {
        return
      }
      if (dataForm.value.content && dataForm.value.content.length > 20000) {
        return ElMessage({
          message: $t('product.tinyMceTip'),
          type: 'error',
          duration: 1500
        })
      }
      isSubmit.value = true
      http({
        url: http.adornUrl('/shop/notice'),
        method: dataForm.value.id ? 'put' : 'post',
        data: http.adornData(dataForm.value)
      }).then(() => {
        ElMessage({
          message: $t('publics.operation'),
          type: 'success',
          duration: 1500,
          onClose: () => {
            visible.value = false
            emit('refreshDataList')
            dataForm.value.content = ''
          }
        })
      }).catch(() => {
        isSubmit.value = false
      })
    }
  })
}
defineExpose({ init })
</script>
<style scoped>

@media (max-width: 1660px) {
  .dialogWidth :deep(.el-dialog) {
    width: 56% !important;
  }
}
</style>
